package com.example.review;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class chapterSeven2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_seven2);
        Button button=findViewById(R.id.make_call);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //检查是否有权限  上下文+权限名
                if (ContextCompat.checkSelfPermission(chapterSeven2Activity.this,
                        Manifest. permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED ){
                    //没权限就去申请权限 三个参数，活动实例、权限名字，请求码
                    ActivityCompat.requestPermissions(chapterSeven2Activity.this,new String[]{Manifest. permission.CALL_PHONE},1);
                }else {
                    call();
                }
            }
        });
    }

     public void call(){
        try {
                    Intent intent=new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:10086"));
                    startActivity(intent);
                }catch (SecurityException e){
                    e.printStackTrace();
        }
    }

    //重写onRequestPermissionsResult方法  即根据申请权限的结果做出处理

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                if (grantResults.length >0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    call();
                }else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}



/**
 * 以下没有申请权限
 */

//public class chapterSeven2Activity extends AppCompatActivity {
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_chapter_seven2);
//        Button button=findViewById(R.id.make_call);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                try {
//                    Intent intent=new Intent(Intent.ACTION_CALL);
//                    intent.setData(Uri.parse("tel:10086"));
//                    startActivity(intent);
//                }catch (SecurityException e){
//                    e.printStackTrace();
//                }
//            }
//        });
//    }
//}